<template>
	<view class="content">
		<view class="posterImg">
			<image class="couponBanner" :src="recommendedList[0].activityImgUrl" mode="widthFix"></image>
		</view>
		<view class="couponList">
			<view class="coupon_item" v-for="(item, index) in couponList" :key="index">
				<view class="coupon_item_main">
					<view class="item_left" :style="{'background':getRgb(templateConfig.page.themeColor,0.1)}">
						<view class="icon"  :style="{'background':getRgb(templateConfig.page.themeColor,1)}">
							<text class="couponType" v-if="item.couponType==0" >满减券</text>
							<!-- <text class="couponType" v-else-if="item.couponType==2" >积分券</text> -->
							<text class="couponType"   v-else-if="item.couponType==3" >折扣券</text>
							<!-- <text class="couponType" v-else-if="item.couponType==1" >口令券</text> -->
						</view>
						<view class="amount">
							<view class="money" v-if="item.couponType == 0 || item.couponType == 1">
								<text class="text">￥</text>
								<span class="span">{{item.couponDenomination}}</span>
							</view>
							<view class="money" v-else-if="item.couponType == 2">
								<span class="span">{{item.couponDenomination}}</span>
								<text class="text">积分</text>
							</view>
							<view class="money" v-else-if="item.couponType == 3">
								<span class="span">{{item.couponDenomination * 10}}</span>
								<text class="text">折</text>
							</view>
						</view>
						
						<view class="desc" v-if="item.couponType == 0 || item.couponType == 1 || item.couponType == 3">
							<text>满{{item.howMuchFullMoney}}可用</text>
						</view>
					</view>
					<view class="item_right">
						<view class="right_left">
							<text class="desc">{{item.couponName}}</text>
							<text class="date" v-if="item.timeType == 1">{{setDate(item)}}内有效</text>
							<text class="date" v-if="item.timeType == 2">领取后{{item.timeDay}}天内有效</text>
						</view>
						<view class="couponType" >
							<text class="text1" :style="{'background':getRgb(templateConfig.page.themeColor,1)}" v-if="item.goodsType == 1">全部商品可用</text>
							<text class="text1" :style="{'background':getRgb(templateConfig.page.themeColor,1)}" v-if="item.goodsType == 2">指定商品可用</text>
						</view>
						<!-- <view class="avaliable">
							<text class="text2">每人可领取{{item.maxNumber}}张</text>
						</view> -->
						<view class="bottom">
							<!-- <view class="item" @click="clickItem(index)">
								<text>展开详情</text>
								<u-icon name="arrow-down" color="#9d9d9d" size="14"></u-icon>
							</view -->
							<text class="text2">每人可领取{{item.maxNumber}}张</text>
							<view class="right_right">
								<button 
									:disabled="item.receiveStatus" 
									class="button" 
									:class="[item.receiveStatus?'received':'unreceived']"
									:style="{'background':item.receiveStatus?getRgb(templateConfig.page.themeColor,0.3):getRgb(templateConfig.page.themeColor,1)}"
									@click="getCoupon(item)"
								>
									<text >{{item.receiveStatus?'已领取':'领取'}}</text>
								</button>
							</view>
							
						</view>
						
					</view>
				</view>
				<view class="slide_dowm" :class="[couponIndex==index?'show':'hide']">
					<u-parse :content="item.detail"></u-parse>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {
		searchPlugsCouponsByBusinessId,getRecommen,receiveCoupons
	} from '../../../static/utils/api.js';
	import { getRgb } from '../../../static/utils/utils.js';

export default {
	data() {
		return {
			templateConfig: uni.getStorageSync('templateConfig'),
			recommendedList:[],
			nowBusinessInfo:{},
			couponList: [],
			orderPage: {
				page: 1,
				sizes: 10,
				total: 0
			},
			
		};
	},
	onLoad() {
		
	},
	computed: {
		setDate () {
			return function (item) {
				if (!item.startTime || !item.endTime) return; 
				let dateStr = '';
				let startTime = item.startTime.split(' ')[0];
				let endTime = item.endTime.split(' ')[0];
				return startTime + ' 至 ' +  endTime;
			}
		}
	},
	onShow() {
		this.nowBusinessInfo = uni.getStorageSync('nowBusinessInfo');
		this.getswiperList()
		this.getcouponList();
	},
	onReachBottom() {
		let self = this;
		if (self.orderPage.total > self.orderPage.page) {
			self.orderPage.page++;
			self.getcouponList();
		}
	},
	methods: {
		getRgb,
		Topath(route) {
			uni.navigateTo({
				url: route
			});
		},
		getcouponList() {
			let postdata = {
				businessId:  this.nowBusinessInfo.businessId.toString(),
				pageNum: this.orderPage.page,
				pageSize: this.orderPage.sizes,
				userServiceToken: this.userToken
			};
			searchPlugsCouponsByBusinessId(postdata).then((res) => {
				if (res.code == 200) {
					this.couponList = res.body.list;
				}
			});
		},
		// 获取轮播
		getswiperList() {
			let data = {
				businessId: this.nowBusinessInfo.businessId.toString()
			};
			getRecommen(data).then(res => {
				if (res.code == 200) {
					let arr1 = [];
					arr1 = res.body.filter(itm => itm.type === 'coupon');
					this.recommendedList = [...arr1];
				}
			});
		},
		getCoupon(item) {
			console.log(123);
			let that = this;
			let token = uni.getStorageSync('userInfo').accessToken;
			if (token) {
				receiveCoupons({ couponsId: item.id })
				.then((res) => {
					console.log(res,12344);
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '领取成功'
						});
						that.getcouponList();
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
				.catch((err)=>{
					console.log(err);
				})
			} else {
				uni.showModal({
					title: '提示',
					content: '您还未登录,登录才能领取哦!',
					success: function (res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/user/login'
							});
						} else if (res.cancel) {
						}
					}
				});
			}
		},
		
	}
};
</script>

<style lang="scss">
.content {
	padding-bottom: 20rpx;
	.couponList{
		width: 100%;
		padding: 20rpx;
		box-sizing: border-box;
		
		.coupon_item{
			width: 100%;
			display: flex;
			flex-direction: column;
			margin-bottom: 20rpx;
			
			.coupon_item_main{
				width: 100%;
				height: 250rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				overflow: hidden;
				
				.item_left{
					width: 30%;
					height: 100%;
					background-color: #FFE3E5;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					position: relative;
					border-radius: 20rpx;
					
					.icon{
						position: absolute;
						top: 0;
						left: 0;
						border-top-left-radius: 20rpx;
						border-bottom-right-radius: 20rpx;
						background-color: #FF1414;
						color: #fff;
						font-size: 24rpx;
						padding: 6rpx 20rpx;
					}
					
					.amount{
						
						.money{
							display: flex;
							
							.text{
								color: #FF1414;
								display: flex;
								align-items: flex-end;
								padding-bottom: 8rpx;
							}
							.span{
								font-size: 64rpx;
								font-weight: bold;
								color: #FF1414;
							}
						}
					}
					
					.desc{
						color: #FF1414;
						font-size: 26rpx;
					}
					
					
					
				}
				.item_right{
					height: 100%;
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					background-color: #fff;
					border-radius: 20rpx;
					padding: 20rpx 20rpx;
					box-sizing: border-box;
					
					.couponType{
						
						.text1{
							border-radius: 14rpx;
							background-color: #FF1414;
							color: #fff;
							font-size: 24rpx;
							padding: 6rpx 10rpx;
						}
						.text2{
							border-radius: 14rpx;
							background-color: #01cb88;
							color: #fff;
							font-size: 24rpx;
							padding: 6rpx 10rpx;
						}
					}
					
					.avaliable{
						
						text{
							font-size: 24rpx;
							color: #b8b8b8;
						}
					}
					
					.right_left{
						display: flex;
						flex-direction: column;
						
						.desc{
							font-size: 34rpx;
							font-weight: bold;
							padding-bottom: 10rpx;
						}
						.date{
							font-size: 24rpx;
							color: #747474;
							font-weight: bold;
						}
					}
					
					
					.bottom{
						display: flex;
						justify-content: space-between;
						align-items: center;
						
						.item{
							display: flex;
							color: #9d9d9d;
							font-size: 26rpx;
						}
						.right_right{
							display: flex;
							align-items: center;
							
							.button{
								width: 130rpx;
								height: 50rpx;
								border-radius: 25rpx;
								font-size: 28rpx;
								color: #fff;
								padding: 0;
							}
							.received{
								background-color: #01cb88;
								display: flex;
								justify-content: center;
								align-items: center;
							}
							.unreceived{
								background-color: #01cb88;
								display: flex;
								justify-content: center;
								align-items: center;
							}
							.unreceived:active{
							}
						}
					}
					
				}
				
			}
			.slide_dowm{
				width: 100%;
				background-color: #fff;
				font-size: 28rpx;
				color: #7e7e7e;
				padding: 30rpx;
				box-sizing: border-box;
				border-bottom-left-radius: 20rpx;
				border-bottom-right-radius: 20rpx;
				// position: absolute;
				// bottom: 0;
			}
			.show{
				display: block;
				animation-name: show;
				animation-duration: 0.4s;
				animation-fill-mode: forwards;
			}
			.hide{
				display: none;
				animation-name: hide;
				animation-duration: 0.4s;
				animation-fill-mode: forwards;
			}
			@keyframes show {
				from{
					opacity: 0;
				} to {
					opacity: 1;
				}
			}
			@keyframes hide {
				from{
					opacity: 1;
				} to {
					opacity: 0;
				}
			}
			
		}
	}
	
}
page {
	background-color: #f5f5f5;
}
.common {
	width: 94%;
	margin: auto;
}
.couponActive {
	display: flex;
	justify-content: space-between;
	margin: 30rpx auto;
	align-items: center;
	.couponActive-left {
		font-weight: bold;
	}
	.couponActive-right {
		display: flex;
		align-items: center;
		color: #6d6d6d;
	}
}
.coupon {
	.couponBox {
		margin-bottom: 40rpx;
		.coupon-title {
			background-color: #e70013;
			font-size: 22rpx;
			width: 20%;
			text-align: center;
			color: #fff;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			padding: 4rpx 16rpx;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
		}
	}
}
</style>
